﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krilib.Data;
using Krilib.Control.Surface;

namespace Krile.Data
{
    internal static class DefaultCommandButtons
    {

        internal static CommandButton GetFavButton(Status target)
        {
            CommandButtonBasicImpl fav = new FavButton(
                target, Properties.Resources.star, Lang.DefaultCommandButton.SetFavor);
            fav.OnMouseClicked += new Action<ChainMouseEventArgs>((e) =>
            {
                Action act = new Action(() =>
                {
                    fav.Enabled = false;
                    Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favouring);
                    Core.FormManager.MainForm.RefreshTL(false);
                    var or = target.ParentAgent.Status.SetFavorite(target.UID, !target.favored);
                    if (or.Succeeded)
                    {
                        target.favored = !target.favored;
                    }
                    else
                    {
                        Subsystem.Debugger.AddReport(new Krilib.Bridges.DebugReport("Fav operation was failed at " + target.UID.ToString()));
                        Core.FormManager.MainForm.SetStatusText(Lang.StatusText.FavError, 3);
                    }
                    fav.Enabled = true;
                    Core.FormManager.MainForm.RefreshTL(true);
                    Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favored, 3);
                });
                act.BeginInvoke((iar) => ((Action)iar.AsyncState).EndInvoke(iar), act);
            });
            return fav;
        }

        internal static CommandButton GetReplyButton(Status target)
        {
            var rep = new CommandButtonBasicImpl(Properties.Resources.comment_add, Lang.DefaultCommandButton.SetReply);
            rep.OnMouseClicked += new Action<ChainMouseEventArgs>((e) => Core.FormManager.MainForm.InputControlManager.SetReplyTo(target.UID));
            return rep;
        }
    }

    internal class FavButton : CommandButtonBasicImpl
    {
        Status parent;
        internal FavButton(Status parent, System.Drawing.Image img, String desc)
            : base(img, desc)
        { 
        this.parent = parent;
        }

        public override bool Checked { get { return parent.favored; } set { } }
    }
}
